package cn.cvs.service;

import cn.cvs.dao.sysUser.CardMapper;
import cn.cvs.pojo.Card;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class CardService  {
    /**
     *查询全部信息
     * @return
     */
    public List<Card> list(@Param("pageBegin")Integer pageBegin,@Param("pageSize")Integer pageSize){
        SqlSession sqlSession =null;
        List<Card> list=new ArrayList<Card>();
        try {
            sqlSession=MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(CardMapper.class).list(pageBegin, pageSize);
        }catch (Exception e){
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;

    }
    /**
     * 添加门禁卡信息
     * @Param("userName")String userName, @Param("idNumber")String idNumber, @Param("createDate")Date createDate
     * @return
     */
    public int add(Card card){
        SqlSession sqlSession =null;
        int count =0;
        try{
            sqlSession= MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(CardMapper.class).add(card);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }
    /**
     * 根据id修改启用状态
     * @param status
     * @param id
     * @return
     */
    public int update(@Param("status")int status, @Param("id")Integer id){
        SqlSession sqlSession =null;
        int count =0;
        try{
            sqlSession =MyBatisUtil.createSqlSession();
            count=sqlSession.getMapper(CardMapper.class).update(status,id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }
    /**
     * 根据id删除门禁卡信息
     * @param id 要删除用户门禁卡的id
     * @return
     */
    public int del(@Param("id")Integer id){
        SqlSession sqlSession =null;
        int count =0;
        try{
            sqlSession =MyBatisUtil.createSqlSession();
            count=sqlSession.getMapper(CardMapper.class).del(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }
    /**
     * 查看门禁卡详细信息
     * @param idNumber
     * @return
     */
    public Card details(@Param("idNumber")String idNumber){
        SqlSession sqlSession =null;
        Card card =new Card();
        try{
            sqlSession =MyBatisUtil.createSqlSession();
            card = sqlSession.getMapper(CardMapper.class).details(idNumber);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return card;
    }
}
